package com.campus.controller.normal;

import com.campus.pojo.dto.SubjectDTO;
import com.campus.pojo.vo.SubjectVO;
import com.campus.result.Result;
import com.campus.service.SubjectService;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RequestMapping("/subject")
@RestController
@Tag(name = "学科管理")
@RequiredArgsConstructor
@CrossOrigin //允许跨域
public class SubjectController {

    private final SubjectService subjectService;

    /**
     * 获取全部科目
     * @return
     */
    @GetMapping
    public Result getAllSubjects(){
        log.info("获取所有学科");
        List<SubjectVO> subjects = subjectService.getAllSubjects();
        return Result.success(subjects);
    }

    /**
     * 根据id删除学科
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result deleteSubject(@PathVariable Integer id){
        log.info("删除学科，id:{}",id);
        subjectService.deleteSubject(id);
        return Result.success();
    }

    /**
     * 添加学科
     * @param subject
     * @return
     */
    @PostMapping
    public Result addSubject(@RequestBody SubjectDTO subject){
        log.info("添加学科{}", subject);
        subjectService.addSubject(subject);
        return Result.success();
    }

    /**
     * 更新学科
     * @param subject
     * @return
     */
    @PutMapping
    public Result updateSubject(@RequestBody SubjectDTO subject){
        log.info("更新学科{}", subject);
        subjectService.updateSubject(subject);
        return Result.success();
    }
}
